import router from "@/router";
import { useGrantsStore } from "@/stores/grantsStore";

// 1、定义所有的（跟权限相关的）路由

const allRoutes = [
  {
    id: "1",
    path: "",
    redirect: "/Admin/Home",
    meta:{
      titles:[]
    }
  },
  {
    id: "1",
    name: "Home",
    path: "Home",
    component: () => import("@/views/Admin/Home/index.vue"),
    meta:{
      titles:[]
    }
  },
  {
    id: "2-1",
    name: "Banner",
    path: "Banner",
    component: () => import("@/views/Admin/Banner/index.vue"),
    meta:{
      titles:["轮播图管理","轮播图列表"]
    }
  },
  {
    id: "3-1",
    name: "Operator",
    path: "Operator",
    component: () => import("@/views/Admin/Operator/index.vue"),
    meta:{
      titles:["操作人员管理","操作人员列表"]
    }
  },
  {
    id: "4-1",
    name: "ProductList",
    path: "ProductList",
    component: () => import("@/views/Admin/Pro/ProList.vue"),
    meta:{
      titles:["产品管理","产品列表"]
    }
  },
  {
    id: "4-2",
    name: "Seckill",
    path: "Seckill",
    component: () => import("@/views/Admin/Pro/ProSecKill.vue"),
    meta:{
      titles:["产品管理","秒杀列表"]
    }
  },
  {
    id: "4-4",
    name: "Search",
    path: "Search",
    component: () => import("@/views/Admin/Pro/ProSearch.vue"),
    meta:{
      titles:["产品管理","筛选列表"]
    }
  },
  {
    id: "4-5",
    name: "Category",
    path: "Category",
    component: () => import("@/views/Admin/Pro/ProCategory.vue"),
    meta:{
      titles:["产品管理","分类列表"]
    }
  },
  {
    id: "5",
    name: "MapDemo",
    path: "MapDemo",
    component: () => import("@/views/Admin/MapDemo/index.vue"),
    meta:{
      titles:["地图管理"]
    }
  },
  {
    id: "6",
    name: "EchartsDemo",
    path: "EchartsDemo",
    component: () => import("@/views/Admin/EchartsDemo/index.vue"),
    meta:{
      titles:["数据统计"]
    }
  },
  {
    id: "7",
    name: "RichTextDemo",
    path: "RichTextDemo",
    component: () => import("@/views/Admin/RichTextDemo/index.vue"),
    meta:{
      titles:["富文本"]
    }
  },
  {
    id: "8",
    name: "MyVModel",
    path: "MyVModel",
    component: () => import("@/views/Admin/MyVModel/index.vue"),
    meta:{
      titles:["自定义组件的v-model"]
    }
  }

  
];

//2、根据当前用户的权限（checkedKeys）过滤出，对应的路由配置。

export const getCurrRoutes = () => {
  const { isDoGrantsed } = useGrantsStore();
  isDoGrantsed();//把仓库的isDoGrants改为true。

  // 1、获取当前用户的权限checkedKeys

  let checkedKeys = JSON.parse(sessionStorage.getItem("checkedKeys") || "[]");
  // ["1","2","2-1","4","4-2","4-3"]
  if (checkedKeys.length === 0) {
    checkedKeys = ["3", "3-1"];
  }

  // 2、过滤

  let routes = allRoutes.forEach((item) => {
    if (checkedKeys.includes(item.id)) {
      //2-1
      // 处理crud：
      let crud = ["0", "0", "0", "0"];
      if (checkedKeys.includes(item.id + "-C")) {
        crud[0] = "1";
      }
      if (checkedKeys.includes(item.id + "-R")) {
        crud[1] = "1";
      }
      if (checkedKeys.includes(item.id + "-U")) {
        crud[2] = "1";
      }
      if (checkedKeys.includes(item.id + "-D")) {
        crud[3] = "1";
      }

      // item.meta = { crud: crud.join("") };
      item.meta.crud = crud.join("");

      // 给Admin添加子级路由配置
      router.addRoute("Admin", item);

    }
  });
};

// ▸ addRoute(parentName, route): () => void

// 添加一个新的路由记录，将其作为一个已有路由的子路由。

export const resetRoutes = () => {
  
}